package com.hbnu.iostream;

import java.io.*;

/**
 * 需求：通过字符输入流读取指定文件的内容，并输出到控制台中
 *
 * @author kittychen
 * @since 2025-10-13 16:33
 */
public class ReaderDemo {
    public static void main(String[] args) {
        // 1、准备文件对象
        String path = "C:" + File.separator + "Users" + File.separator + "Administrator"
                + File.separator + "Desktop" + File.separator + "kittychen"
                + File.separator + "class2311" + File.separator + "writer.txt";
        File file = new File(path);

        Reader reader = null;

        try {
            reader = new FileReader(file);
            char[] buffer = new char[1024];
            // int len = reader.read(buffer);

            int len = 0;
            int temp = 0;
            while ((temp = reader.read()) != -1) {
                buffer[len++] = (char) temp;
            }

            System.out.println(new String(buffer, 0, len));

        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                if (reader != null) {
                    reader.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }


    }
}
